Skip to content

Clarify as_uninit_mut may point to uninitialized memory#158804

Open
yilin0518 wants to merge 1 commit into
rust-lang:mainfrom
yilin0518:fix_as_uninit_mut
Open

Clarify as_uninit_mut may point to uninitialized memory#158804
yilin0518 wants to merge 1 commit into
rust-lang:mainfrom
yilin0518:fix_as_uninit_mut

Conversation

@yilin0518

@yilin0518 yilin0518 commented Jul 5, 2026

Copy link
Copy Markdown
Contributor

Tracking issue: #75402

This PR clarifies the safety documentation for *mut T::as_uninit_mut.

Unlike as_mut, this method creates a reference to MaybeUninit<T>, so the source pointer may point to uninitialized memory. The pointer still must either be null or satisfy the pointer-to-reference conversion requirements.

After adding the supplementary information, the safety documentation of *mut T::as_uninit_mut is consistent with *mut T::as_uninit_ref, *const T::as_uninit_ref and std::ptr::NonNull::as_uninit_mut.

This is a documentation-only change.

Tests were not added because this only updates API docs.

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Jul 5, 2026
@rustbot

rustbot commented Jul 5, 2026

Copy link
Copy Markdown
Collaborator

r? @LawnGnome

rustbot has assigned @LawnGnome.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: libs
  • libs expanded to 12 candidates
  • Random selection from 6 candidates

/// When calling this method, you have to ensure that *either* the pointer is null *or*
/// the pointer is [convertible to a reference](crate::ptr#pointer-to-reference-conversion).
/// Note that because the created reference is to `MaybeUninit<T>`, the
/// source pointer can point to uninitialized memory.

@hkBst hkBst Jul 8, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This formulation seems backwards to me, and this information also seems already to be included in "In contrast to [as_mut], this does not require that the value has to be initialized." which is a few lines above this.

View changes since the review

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The purpose of the change I made is let the safety documentation of this API consistent with other APIs(*mut T::as_uninit_ref, *const T::as_uninit_ref and std::ptr::NonNull::as_uninit_mut). All these APIs has this pattern in their safety documentation. Although "In contrast to [as_mut], this does not require that the value has to be initialized." has implied the uninitiazlition, it seems that explicitly emphasize this in #Safety is better. And thank you for your review and feedback!

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see. Also, you're welcome to the review and thanks for trying to improve the docs and their consistency in particular.

I'm not sure I agree this needs to be repeated in the SAFETY section, as it is not a safety condition that will have to be dispatched in a safety comment using these methods. The MaybeUninit type already makes it very difficult to accidentally mishandle MaybeUninit<T> as T.

Anyway, assuming something like this is included in the SAFETY section, I would prefer a reformulation. From "Note that because the created reference is to MaybeUninit<T>, the source pointer can point to uninitialized memory." to perhaps one of:

  • Remember that the returned reference may refer to an uninitialized T.
  • Note: returned reference may refer to an uninitialized T.
    Or if the point is instead about the argument:
  • self may point to uninitialized memory.

What do you think?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From "Note that because the created reference is to MaybeUninit<T>, the source pointer can point to uninitialized memory." to perhaps one of:

  • Remember that the returned reference may refer to an uninitialized T.
  • Note: returned reference may refer to an uninitialized T.
    Or if the point is instead about the argument:
  • self may point to uninitialized memory.

What do you think?

Well I think reformulation is optional, but adding the uninitialization hint in SAFETY section is better than in other place, because SAFETY section needs to remind developers that what they should care about when using this API.

But I think the first discription is great if needing reformulation. It is more concise. And the other point we should care is that the documentation of these APIs need modification if reformulation is accepted. I can finfish the work and apply in this PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants